package pd.struct;

/**
 * A pair of nodes that describes an operation mapping. This is a protected class.
 *
 * @author Joshua Swank
 */
class OperationPair
{
    /**
     * A node of the operation pair.
     */
    private ScriptNode a, b;

    /**
     * Represents an operation type.
     *
     * @author Joshua Swank
     */
    protected enum Operation
    {
        /**
         * Represents an overwrite.
         */
        OVERWRITE( 1.0 ),

        /**
         * Represents an insertion.
         */
        INSERTION( 1.0 ),

        /**
         * Represents a deletion.
         */
        DELETION( 1.0 ),

        /**
         * Represents an invalid operation pair.
         */
        INVALID( 0.0 );

        /**
         * The cost of the operation.
         */
        protected final double cost;

        /**
         * Specifies the weight of each operation.
         * 
         * @param cost the weight of the operation.
         */
        private Operation( double cost )
        {
            this.cost = cost;
        }
    }

    /**
     * Initializes a new operation pair with the specified nodes.
     *
     * @param a the first node of the pair.
     * @param b the second node of the pair.
     */
    protected OperationPair( ScriptNode a, ScriptNode b )
    {
        this.a = a;
        this.b = b;
    }

    protected double getCost()
    {
        return getType().cost;
    }

    /**
     * Returns the type of operation described by the operation pair.
     *
     * @return the type of operation described by the operation pair.
     */
    protected Operation getType()
    {
        if( a != null )
        {
            if( b != null )
                return Operation.OVERWRITE;
            else
                return Operation.DELETION;
        } else {
            if( b != null )
                return Operation.INSERTION;
            else
                return Operation.INVALID;
        }
    }
}
